# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
import re
import uuid
from urllib import request
import os

class VipspiderSpider(scrapy.Spider):
    name = 'vipspider'
    allowed_domains = ['vip.com']
    start_urls = ['http://vip.com/']

    def parse(self, response):
        ##################填写关键@@@  64
        keyword = "短裤"
        for page in range(0,30):
            url = "https://category.vip.com/suggest.php?keyword="+keyword+"&page="+str(page)
            yield Request(url=url,callback=self.itemlist)

    def itemlist(self,response):
        body = response.body.decode("utf-8","ignore")

        pat_product_id = '"product_id":"(\d+)'
        pat_brand_id = '"brand_id":"(\d+)'
        all_product_id = re.compile(pat_product_id).findall(body)
        all_brand_id = re.compile(pat_brand_id).findall(body)
        for i in range(0,len(all_brand_id)):
            url = "https://detail.vip.com/detail-"+str(all_brand_id[i])+"-"+all_product_id[i]+".html"
            yield Request(url=url,callback=self.detail_img)

    def detail_img(self,response):
        body = response.body.decode("utf-8", "ignore")
        pat_imgurl = '<img src="//s2.vipstatic.com/img/share/blank.png" data-original="(.*?)"'
        imgurl_list = re.compile(pat_imgurl).findall(body)
        ##########存放位置############
        filepath = "C:/Users/Administrator/Desktop/短裤/"
        if not os.path.exists(filepath):  # 判断该路径下是否存在该文件夹
            os.makedirs(filepath)
        for imguir in imgurl_list:
            imgurl = "http:"+imguir
            filename = filepath + '/' + str(uuid.uuid1()) + '.jpg'
            request.urlretrieve(imgurl,filename)#保存图片的过程




